package com.ubs.training.web.controller;

import com.ubs.training.prod.server.service.LoggingService;
import com.ubs.training.server.dao.model.Employee;
import com.ubs.training.server.dao.model.UBSContact;
import com.ubs.training.server.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/22/12
 * Time: 10:18 AM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class UBSContactController {
    @Autowired
    UBSContactService contactService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    LoggingService log;
    @Autowired
    ContactTypeService cTypeService;


    @RequestMapping(value = "client/{clientId}/ubscontact/list.do", method = RequestMethod.GET)
    public String listUbsContact(Model model, @PathVariable int clientId) {
        model.addAttribute("ubsContact", contactService.getAllUBSContactByClientId(clientId));
        model.addAttribute("ubsEmpl", contactService.getIdWithFirstLastName(clientId));
        model.addAttribute("clientId", clientId);
        return "listUbsContact";
    }

    @RequestMapping(value = "client/{clientId}/ubscontact/{ubsFLNameEmployeeid}/edit.do", method = RequestMethod.GET)
    public String getClient(@PathVariable("clientId") int clientId,@PathVariable("ubsFLNameEmployeeid") int ubsFLNameEmployeeId, Model model) {
        UBSContact ubsContact;
        if(ubsFLNameEmployeeId > 0) {
            ubsContact = contactService.getUBSContactById(ubsFLNameEmployeeId);
        } else {
            ubsContact = new UBSContact();
        }
        ubsContact.setClient_id(clientId);
        List<Employee> emplList= employeeService.getAllEmployee();
        model.addAttribute("ubsContact", ubsContact);
        model.addAttribute("employee", emplList);
        model.addAttribute("cType", cTypeService.getAllContactType());
        model.addAttribute("clientId", clientId);
        return "editUbsContact";
    }

    @RequestMapping(value = "client/{clientId}/ubscontact/{ubsFLNameEmployeeid}/edit.do", method = RequestMethod.POST)
    public String saveUbsContact(@PathVariable("clientId") int clientId, @ModelAttribute("UbsContact") UBSContact ubsContact, BindingResult res) {
        //new ClientValidator(ubsContact, res).validate();

        ubsContact.setId(ubsContact.getId());
        if(res.hasErrors()) {
            return "editUbsContact";
        }
        contactService.saveUBSContact(ubsContact);

        return "redirect:/client/{clientId}/ubscontact/list.do";
    }

    @RequestMapping(value = "client/{clientId}/ubscontact/{ubsId}/delete.do", method = RequestMethod.GET)
    public String deleteContact(@PathVariable("ubsId") int ubsId, @PathVariable("clientId") int clientId, Model model) {
        model.addAttribute("clientId", clientId);
        model.addAttribute("ubsId", ubsId);
        contactService.deleteUBSContact(ubsId);
        return "redirect:/client/{clientId}/ubscontact/list.do";
    }


}
